<?php

namespace app\common\model;

use think\Cache;
use think\Model;

class CommonBase extends Model{

    const CACHE_TIME = 31536000;
    const MGJYXJL = 'https://emweb.securities.eastmoney.com/PC_HSF10/NewFinanceAnalysis/ZYZBAjaxNew?type=0&code='; //每股经营现金流

    public static function cSet($name='',$v='',$time=0){

        if($name==''|| $v==''){return false;}
        if($time<=0){$time=self::CACHE_TIME;}
        Cache::set($name,$v,$time);
    }

    public static function cGet($name=''){

        if($name==''){return false;}
        return Cache::get($name);
    }

    public static function cRm($name=''){

        if($name==''){return false;}
        return Cache::rm($name);
    }

    public static function cPull($name=''){

        if($name==''){return false;}
        return Cache::rm($name);
    }

    public static function cClear(){

        Cache::clear(); 
    }

    public static function cGetData(){

        Cache::remember('name',function(){
            return time();
        });
    }

    //动态市盈率(TTM)
    public static function stockTTM($jys='', $stockCode=''){

        if($jys=='' || $stockCode==''){return false;}

        $stockBase = file_get_contents('https://gu.qq.com/'.$jys.$stockCode.'/gp');
        //TTM滚动市盈率
        $stock_cache_key = $jys.$stockCode.'_price_earnings_ratio_ttm';
        $stock_cache_ttm = false;
        $stock_cache_ttm = self::cGet($stock_cache_key);
        if($stock_cache_ttm==false){

            preg_match('/市盈率:<\/span><span class="col-2-4 bl" data-reactid=".*?">(.*?)<\/span>/', $stockBase, $price_earnings_ratio_ttm);
            $stock_cache_ttm = $price_earnings_ratio_ttm[1];
            self::cSet($stock_cache_key,$stock_cache_ttm);
            unset($stockBase);
            unset($price_earnings_ratio_ttm);
        }
        return $stock_cache_ttm;
    }

    //获取当前股价
    public static function getCurrentStockPrice($stockCode=''){

        if($stockCode==''){return false;}

        $currentTime = time();
        $currentDate = date('Y-m-d');
        $fifteenTime = strtotime($currentDate.' 15:10:00');
        $thirteenTime= strtotime($currentDate.' 13:00:00');
        $elevenThirty= strtotime($currentDate.' 11:30:00');
        $nineThirty  = strtotime($currentDate.' 09:30:00');

        $price = '';
        if($nineThirty <= $currentTime && $currentTime<=$elevenThirty){
            $price = self::stockPrice($stockCode);
        }elseif($thirteenTime <= $currentTime && $currentTime<=$fifteenTime){

            $price = self::stockPrice($stockCode);
        }else{

            $stock_cache_key = 'jys_stock_price_all_'.$_SERVER['REQUEST_URI'];
            $price = self::cGet($stock_cache_key);
            if($price==false || $price==''){

                $price = self::stockPrice($stockCode);
                self::cSet($stock_cache_key,$price,7100);
            }
        }
        return $price;
    }

    //获取当前实时股价
    public static function stockPrice($stockCode='',$type=0){

        $reVal = [];
        $realTimePrice = file_get_contents('http://qt.gtimg.cn/q='.$stockCode);
        if($type==0){

            preg_match_all('/v_(.*?";)/', $realTimePrice, $tmpVal);
            if(!empty($tmpVal[1])){

                foreach($tmpVal[1] AS $v){

                    $realTimePrice = [];
                    $realTimePrice = explode('~', $v);
                    $reVal[] = [
                        'stock_code' => $realTimePrice[2],
                        'price' => $realTimePrice[3],
                    ];
                }
            }
        }elseif($type==1){
            $realTimePrice = explode('~', $realTimePrice);
            $reVal = $realTimePrice[3];
        }
        return $reVal;
    }

    //每股经营现金流
    public static function epcf($jys='', $stockCode=''){
        
        if($jys=='' || $stockCode==''){return false;}

        $stock_cache_key = $jys.$stockCode.'_epcf';
        $stock_cache_epcf = false;
        $stock_cache_epcf = self::cGet($stock_cache_key);
        if($stock_cache_epcf==false){

            $ZqPrice = file_get_contents(self::MGJYXJL.$jys.$stockCode);
            $current_encode = mb_detect_encoding($ZqPrice, array("ASCII","GB2312","GBK",'BIG5','UTF-8'));
            $baseTmpHandle = '';
            $baseTmpHandle = mb_convert_encoding($ZqPrice, 'UTF-8', $current_encode);
            $baseTmpHandle = json_decode($baseTmpHandle, true);
            self::cSet($stock_cache_key,$baseTmpHandle);
        }
        return $stock_cache_epcf;
    }
}